/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 *  The greatest achievement is selflessness. The greatest worth is self-mastery.
 *  The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 *  The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 *  The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 *  The greatest goodness is a peaceful mind. The greatest patience is humility.
 *  The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 *  The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 * 
 * @author Ashin Wimalajeewa (ash)
 */
package org.javaongems.std.client;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.javaongems.std.client.util.StrTokenizer;

final public class HistoryUtils {
	final static public char COMMA_TOK = ',';
	final static public String HASH_TOK = "#";
	final static public String QUERY_TOK = "" + COMMA_TOK;
	final static public char AMP_TOK = '&';
	final static public char EQUAL_TOK = '=';
	
	private HistoryUtils() {}
	
	static public String parseToken(String tok, Map queryMapStore) {
		tok = UriEncoderUtils.decodeURIComponent(tok);
		int queryPos = tok.indexOf(QUERY_TOK);	// QUERY_TOK
		if (queryPos > -1) {
			String queryStr = tok.substring(queryPos+1);
			StrTokenizer eq = new StrTokenizer(queryStr, EQUAL_TOK);
			StrTokenizer amp = new StrTokenizer(queryStr, AMP_TOK);
			while (amp.hasMoreTokens()) {
				String entry = amp.nextToken();
				eq.reset(entry);
				String name = null, val = null;
				if (eq.hasMoreTokens()) {
					name = eq.nextToken();
					if (name == null || "".equals(name))
						continue;
				}
				if (eq.hasMoreTokens())
					val = eq.nextToken();
				queryMapStore.put(name, val);
			}
			return tok.substring(0, queryPos);
		} else
			return tok;
	}
	
	static public String toHistoryToken(String label, Map queryMap) {
		if (queryMap == null || queryMap.size() == 0)
			return label;
		StringBuffer ret = new StringBuffer(label);
		ret.append(QUERY_TOK);
		boolean first = true;
		for (Iterator iter = queryMap.keySet().iterator(); iter.hasNext();) {
			String key = (String) iter.next();
			String val = queryMap.get(key).toString();
			if (!first) {
				ret.append(AMP_TOK);
			} else
				first = false;
			ret.append(key);
			ret.append(EQUAL_TOK);
			ret.append(val);
		}
		return ret.toString();
	}
	
	static public String toHistoryToken(String label, String key1, Object value1) {
		HashMap queryMap = new HashMap();
		queryMap.put(key1, value1);
		return toHistoryToken(label, queryMap);
	}
	
	native static public String getQueryString() /*-{
    	return $wnd.location.search;
	}-*/;
	
	native static public String getHash() /*-{
    	return $wnd.location.hash;
	}-*/;
}
